import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bmfmap_example/http/http.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/goodpage/goodpage.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({Key key}) : super(key: key);

  @override
  _SearchState createState() => _SearchState();
}

class _SearchState extends State<SearchPage> {
  var _searchGoods = [];

  _serach(String text) async {
    print(text);
    if (text == '') {
      _searchGoods.clear();
      setState(() {});
      return;
    }
    var response = await Http.get('/commodity/selectByName?nameInfo=' + text);
    _searchGoods = response['list'];
    // this._goods = jsonDecode(response)['list'];
    print(response);

    if (mounted) setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        body: SafeArea(
          child: Container(
            child: Column(
              children: [
                //搜索框
                Padding(
                  padding: const EdgeInsets.only(
                      top: 10.0, left: 15.0, right: 15, bottom: 3),
                  child: TextField(
                    //输入文本样式
                    style: TextStyle(
                        color: Color.fromRGBO(123, 122, 123, 1),
                        fontWeight: FontWeight.bold,
                        fontSize: 20),
                    //光标样式
                    cursorColor: Color.fromRGBO(196, 183, 215, 1),
                    cursorWidth: 2.0, cursorRadius: Radius.circular(4.0),
                    onChanged: (String text) {
                      _serach(text);
                    },
                    //输入框装饰
                    decoration: InputDecoration(
                        //前置图标
                        prefixIcon: Padding(
                          padding: const EdgeInsets.all(16.0),
                          child: Icon(Icons.search),
                        ),
                        //后置图标
                        suffixIcon: IconButton(
                          color: Color.fromRGBO(196, 183, 215, 1),
                          icon: Icon(Icons.clear),
                          onPressed: () => Navigator.pop(context),
                        ),
                        //文本内边距
                        contentPadding: EdgeInsets.all(5.0),
                        //无边框
                        border: InputBorder.none,
                        //填充色为白色
                        fillColor: Colors.white,
                        filled: true,
                        //圆边框
                        focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.white),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        )),
                  ),
                ),
                //搜索列表框
                Expanded(
                  child: Container(
                    //  color: Colors.black,
                    child: ListView(
                      children: _buildsSearchList(),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  _buildsSearchList() {
    //全部
    List<Widget> searchList = [];
    for (int i = 0; i < _searchGoods.length; i++) {
      print(i);
      searchList.add(
        _searchCard(
          name: _searchGoods[i]['name'].toString(),
          commodityId: _searchGoods[i]['commodityId'].toString(),
          imgurl1: _searchGoods[i]['imgurl1'].toString(),
        ),
      );
    }
    if (_searchGoods == null) searchList.add(Container());
    return searchList;
  }

  _searchCard({String name, String commodityId, String imgurl1}) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, right: 16),
      child: InkWell(
        onTap: () {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => GoodPage(
                        id: commodityId,
                        url1: imgurl1,
                        tagname: 'search' + commodityId,
                      )));
        },
        child: Container(
          height: 68,
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              children: [
                Hero(
                  tag: 'search' + commodityId,
                  child: Image.network(imgurl1),
                ),
                SizedBox(width: 8),
                Text(
                  '$name',
                  style: TextStyle(
                      color: Color.fromRGBO(215, 198, 245, 1),
                      fontWeight: FontWeight.w600,
                      fontSize: 16),
                ),
              ],
            ),
          ),
          decoration: new BoxDecoration(
            color: Color.fromRGBO(255, 255, 255, 1),
            //设置四周圆角 角度
            border: Border(
                bottom: BorderSide(
                    color: Color.fromRGBO(231, 234, 237, 1), width: 0.5)),
          ),
        ),
      ),
    );
  }
}
